/**
 * Project Name:canseqback
 * File Name:Department.java
 * Package Name:com.bgi.dx.module.sys.department.service.impl
 * Date:2014-12-24上午10:26:55
 * Copyright (c) 2014, BGI_DX All Rights Reserved.
 *
*/

package com.bgi.uims.module.sys.department.service;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.bgi.uims.common.dao.ICommonDao;
import com.bgi.uims.common.dto.PageVo;
import com.bgi.uims.module.sys.department.dto.Department;
import com.bgi.uims.pub.utils.Constant;


/**
 * 
 * ClassName:DepartmentService 
 * Function: TODO 
 * Reason:	 TODO 
 * @CreateDate:2015年3月23日 上午11:08:40
 * @author   WEN
 * @version  
 * @since    JDK 1.7
 * @see
 */
@Service
@Transactional
public class DepartmentService {

	@Resource
	private ICommonDao commonDao;
	/**
	 * 
	 * @Description：  查询组织
	 * @author: WEN
	 * @CreateDate:2015年3月23日 上午11:04:50
	 * @version: V1.0
	 */
	public PageVo queryDepartmentByCondition(Department department) {
		PageVo pageVo=new PageVo();
		department.setQuerySql(Constant.SQL_QUERY_DEPARTMENT_CONDITION);
		pageVo=commonDao.query(department);
		return pageVo;
	}

	/**
	 * 
	 * @Description： 加载组织 ，没有查询条件返回第一级
	 * @author: WEN
	 * @CreateDate:2015年3月23日 上午11:04:19
	 * @version: V1.0
	 */
	public PageVo queryDepartment(Department manageDepartment) {
		PageVo pageVo=new PageVo();
		manageDepartment.setQuerySql(Constant.SQL_QUERY_DEPARTMENT);
		pageVo = commonDao.query(manageDepartment);
		return pageVo;
	}
	/**
	 * 
	 * @Description： 删除组织 
	 * @author: WEN
	 * @CreateDate:2015年3月23日 上午11:07:17
	 * @version: V1.0
	 */
	public String deleteDepartment(Department manageDepartment) {
		String resultMsg = "";
		manageDepartment.setDeleteSql(Constant.SQL_DELETE_DEPARTMENT);
		Integer count =  commonDao.delete(manageDepartment);
		if(count == 0){
			resultMsg = Constant.ERROR_CODE_101;
		}
		return resultMsg;
	}

	/**
	 * 
	 * @Description：  更新组织状态
	 * @author: WEN
	 * @CreateDate:2015年3月23日 上午11:06:57
	 * @version: V1.0
	 */
	public String updateDepartmentStatus(Department manageDepartment) {
		String resultMsg = "";
		manageDepartment.setUpdateSql(Constant.SQL_UPDATE_DEPARTMENT_STATUS);
		int count = commonDao.update(manageDepartment);
		if (count == 0){
			resultMsg = Constant.ERROR_CODE_101;
		}
		return resultMsg;
	}

	/**
	 * 
	 * @Description：  保存/更新组织
	 * @author: WEN
	 * @CreateDate:2015年3月23日 上午11:06:35
	 * @version: V1.0
	 */
	public String saveDepartment(Department manageDepartment) {
		String resultMsg = "";
		//查询是否已经存在地区
		manageDepartment.setQueryOneSql(Constant.SQL_QUERY_DEPARTMENT_BYCODE);
		Object resultObject = commonDao.queryOne(manageDepartment);
		int codeCount = resultObject != null ? Integer.parseInt(resultObject.toString()):0;
		manageDepartment.setQueryOneSql(Constant.SQL_QUERY_DEPARTMENT_BYNAME);
		resultObject = commonDao.queryOne(manageDepartment);
		int nameCount = resultObject != null ? Integer.parseInt(resultObject.toString()):0;
		
		boolean falg = true;
		if(nameCount > 0){
			falg = false;
			resultMsg += "组织名称已存在!<br>";
		}
		if(codeCount > 0){
			falg = false;
			resultMsg += "组织编码已存在!";
		}
		if(falg){
			if(manageDepartment.getDepartmentId() != null && !"".equals(manageDepartment.getDepartmentId())){
				manageDepartment.setUpdateSql(Constant.SQL_UPDATE_DEPARTMENT);
				commonDao.update(manageDepartment);
			} else {
				manageDepartment.setInsertSql(Constant.SQL_INSERT_DEPARTMENT);
				commonDao.insert(manageDepartment);
			}
		}
		if(resultMsg.equals("")){
			resultMsg = "true";
		}
		return resultMsg;
	}

	/**
	 * 
	 * @Description：  查询特定的组织
	 * @author: WEN
	 * @CreateDate:2015年3月23日 上午11:06:15
	 * @version: V1.0
	 */
	public PageVo queryDepartmentAssociate(Department manageDepartment) {
		PageVo pageVo=new PageVo();
		manageDepartment.setQuerySql(Constant.SQL_QUERY_DEPARTMENT_ASSOCIATE);
		pageVo=commonDao.query(manageDepartment);
		@SuppressWarnings("unchecked")
		List<Department> departmentObjects = (List<Department>)pageVo.getRows();
		for(Department departmentObject:departmentObjects){
			if (departmentObject.get_parentId() != null && "0".equals(departmentObject.get_parentId()) ){
				departmentObject.set_parentId(null);
			}
		}
		return 	pageVo;
	}

	/**
	 * 
	 * @Description：  获得组织的下级组织
	 * @author: WEN
	 * @CreateDate:2015年3月23日 上午11:06:15
	 * @version: V1.0
	 */
	public PageVo queryLowerDepartmentAssociate(Department department) {
		PageVo pageVo=new PageVo();
		department.setQuerySql(Constant.SQL_QUERY_LOWER_DEPARTMENT_ASSOCIATE);
		pageVo=commonDao.query(department);
		return 	pageVo;
	}

	/**  
	 * @Description：查询单个组织信息  
	 * @author: tantianhua
	 * @CreateDate:2015年4月2日 下午3:19:27
	 * @version: V1.0 
	 */   
	
	public String queryDepartmentNameById(Department department) {
		department.setQueryOneSql(Constant.SQL_QUERY_DEPARTMENTBYID);
		department=(Department) commonDao.queryOne(department);
		String dName = department.getDepartmentName();
		return dName;
	}
	
	/**  
	 * @Description：查询单个组织信息  
	 * @author: tantianhua
	 * @CreateDate:2015年4月2日 下午3:19:27
	 * @version: V1.0 
	 */   
	public Department queryDepartmentById(Department department) {
		department.setQueryOneSql(Constant.SQL_QUERY_DEPARTMENTBYID);
		department=(Department) commonDao.queryOne(department);
		return department;
	}
}

